package com.masterclient.app.modules.goods.newpage.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.bean.goods.CommodityService;
import com.eoner.baselibrary.bean.goods.CouponMessage;

import java.util.List;

public class NewCommodityDetailServiceAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;


    public interface OnServiceCallBack {
        void couponClick();

        void serviceClick();
    }

    OnServiceCallBack onServiceCallBack;

    public OnServiceCallBack getOnServiceCallBack() {
        return onServiceCallBack;
    }

    public void setOnServiceCallBack(OnServiceCallBack onServiceCallBack) {
        this.onServiceCallBack = onServiceCallBack;
    }

    public NewCommodityDetailServiceAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public NewCommodityDetailServiceAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View coupon_layout = holder.itemView.findViewById(R.id.coupon_layout);
        View view_coupon = holder.itemView.findViewById(R.id.view_coupon);
        View service_layout = holder.itemView.findViewById(R.id.service_layout);
        TextView tv_coupon_content = holder.itemView.findViewById(R.id.tv_coupon_content);
        TextView tvGetCoupon = holder.itemView.findViewById(R.id.tv_get_coupon);
        TextView gridLayout = holder.itemView.findViewById(R.id.gridLayout);
        List<CouponMessage> couponMessages = data.getCoupons();

        //优惠券
        if (data.getMeet_reduce_promotion() != null && !TextUtils.isEmpty(data.getMeet_reduce_promotion().getName())) {
            coupon_layout.setVisibility(View.VISIBLE);
            tv_coupon_content.setText(data.getMeet_reduce_promotion().getName());
        } else {
            coupon_layout.setVisibility(View.GONE);
        }

        //领券
        if (couponMessages != null && couponMessages.size() > 0) {
            view_coupon.setVisibility(View.VISIBLE);
            int num = 0;
            StringBuilder couponStr = new StringBuilder();
            for (CouponMessage coupon : couponMessages) {
                if (num != 0) {
                    couponStr.append(" ");
                }
                couponStr.append(coupon.getSubName());
                if (num == 2) {//只显示两条
                    break;
                }
                num++;
            }
            tvGetCoupon.setText(couponStr.toString());
        } else {
            view_coupon.setVisibility(View.GONE);
        }

        coupon_layout.setOnClickListener(view -> {
            if (onServiceCallBack != null) {
                onServiceCallBack.couponClick();
            }
        });
        view_coupon.setOnClickListener(view -> {
            if (onServiceCallBack != null) {
                onServiceCallBack.couponClick();
            }
        });

        //服务
        List<CommodityService> services = data.getServices();
        if (services != null) {
            String str = "";
            int num = 0;
            for (CommodityService service : services) {
                num++;
                if (num == services.size()) {
                    str = str + service.getTitle();
                } else {
                    str = str + service.getTitle() + " · ";
                }
            }
            gridLayout.setText(str);
        }

        service_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onServiceCallBack != null) {
                    onServiceCallBack.serviceClick();
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 3;
    }
}
